package com.zhudeshuai.zhudemolist.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.zhudeshuai.zhudemolist.util.ToastUtil;

/**
 * @author zhudeshuai
 * @since 16/4/13
 */
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private static final String TAG = "MySurfaceView";
    private SurfaceHolder mHolder;
    private Canvas mCanvas;

    /**
     * 用于绘制的线程
     */
    private Thread t;

    /**
     * 线程绘制开关
     */
    private boolean isRunning;


    private int padding = 20;

    private Paint mCirclePaint;

    private Paint mTextPaint;

    private int[] colors = new int[]{android.R.color.holo_orange_light, android.R.color.holo_green_light,
            android.R.color.holo_orange_light, android.R.color.holo_green_light,
            android.R.color.holo_orange_light, android.R.color.holo_green_light,};

    private String[] prize = new String[]{"猪", "狗", "羊", "马", "牛", "猴"};

    /**
     * 开始绘制的位置（单位：弧度）
     */
    private int mStartAngle;
    private float speed;

    /**
     * 是否点击了开奖按钮
     */
    private boolean isOpenPrize = true;


    public MySurfaceView(Context context) {
        this(context, null);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mHolder = getHolder();
        mHolder.addCallback(this);

        setFocusable(true);
        setFocusableInTouchMode(true);

        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setDither(true);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.RED);
        mTextPaint.setTextSize(50);
    }


    public void start() {
        if (isOpenPrize) {
            ToastUtil.showToast("正在开奖");
        } else {
            speed = 20;
            isOpenPrize = true;
            mStartAngle = 0;

        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //surfaceView创建，开启绘制线程
        t = new Thread(this);
        t.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        //surfaceView销毁，关闭绘制线程
        isRunning = false;
    }

    @Override
    public void run() {
        isRunning = true;
        while (isRunning) {
            if (isOpenPrize) {
                doDraw();
            }
        }
    }

    /**
     * 进行绘制操作
     */
    private void doDraw() {
        try {
            mCanvas = mHolder.lockCanvas();
            if (mCanvas != null) {
                long start = System.currentTimeMillis();
                drawSomething();
                //保证每秒最少16帧
                long duration = System.currentTimeMillis() - start;
                if (duration < 16) {
                    Thread.sleep(16 - duration);
                }

            }
        } catch (Exception e) {

        } finally {
            if (mCanvas != null) {
                mHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }

    private void drawSomething() {
        int itemLength = 360 / colors.length;
        mCanvas.drawColor(Color.BLUE);

        float radius = getWidth() / 2 - padding;
        //每个弧长
        float circleLength = (float) (Math.PI * radius * 2) / colors.length;
        //绘制扇形
        RectF rectFBg = new RectF(padding, padding, getWidth() - padding, getHeight() - padding);
        for (int i = 0; i < colors.length; i++) {
            mCirclePaint.setColor(getResources().getColor(colors[i]));
            mCanvas.drawArc(rectFBg, mStartAngle + i * itemLength, itemLength, true, mCirclePaint);

            //绘制文字
            Path path = new Path();
            path.addArc(rectFBg, mStartAngle + i * itemLength, itemLength);
            float textWidth = mTextPaint.measureText(prize[i]);

            mCanvas.drawTextOnPath(prize[i], path, (circleLength - textWidth) / 2, 80, mTextPaint);
        }


        mStartAngle += speed;
        speed -= 0.2;
        if (speed <= 0) {
            isOpenPrize = false;
        } else {
            isOpenPrize = true;
        }


    }
}
